package com.it.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.it.aiReportClaim.ReportClaimApi;
import com.it.entity.denial.Rejection;
import com.it.entity.insure.AiInsureOrder;
import com.it.mapper.RejectionMapper;
import com.it.service.RejectionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import utils.R;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Random;

/**
 * <p>
 * 拒赔表 服务实现类
 * </p>
 *
 * @author yzy
 * @since 2025年09月25日
 */
@Service
public class RejectionServiceImpl extends ServiceImpl<RejectionMapper, Rejection> implements RejectionService {
    @Autowired
    private RejectionMapper rejectionMapper;
    @Autowired
    private ReportClaimApi reportClaimApi;
    @Override
    public R addRejection(Rejection rejection) {
        rejection.setRejectionId(generateRejectionId());
        rejection.setRejectTime(LocalDateTime.now());
        reportClaimApi.updateStatus(rejection.getReportNo(),10);
        rejectionMapper.insert(rejection);
        return R.Success("已申请拒赔");
    }

    @Override
    public R examineRejection(Rejection rejection) {
        rejection.setExamineTime(LocalDateTime.now());
        rejectionMapper.updateById(rejection);
        rejection =rejectionMapper.selectById(rejection.getRejectionId());
        if(rejection.getRejectStatus()==2){
            reportClaimApi.updateStatus(rejection.getReportNo(),9);//拒赔
        }if(rejection.getRejectStatus()==3){
            Rejection rejection1 = rejectionMapper.selectById(rejection.getRejectionId());
             if (rejection1.getPreClaimId()==null||rejection1.getPreClaimId().isEmpty()) {
                reportClaimApi.updateStatus(rejection1.getReportNo(),4);//没有核赔号（立案时拒赔）
            }else {
                reportClaimApi.updateStatus(rejection1.getReportNo(), 7);//有核赔号（核赔时拒赔）
            }
        }
        return R.Success("已审核");
    }

    @Override
    public R pagelist(int pageNum, int pageSize, String reportNo, Integer rejectStatus) {
        // 使用PageHelper设置分页参数
        PageHelper.startPage(pageNum, pageSize);

        // 创建LambdaQueryWrapper，使用Lambda表达式避免硬编码字段名
        LambdaQueryWrapper<Rejection> queryWrapper = new LambdaQueryWrapper<>();

        // 保单编号模糊查询（忽略空值）
        if (StringUtils.isNotBlank(reportNo)) {
            queryWrapper.like(Rejection::getReportNo, reportNo);
        }


        // 保单状态精确匹配（忽略空值）
        if (rejectStatus != null) {
            queryWrapper.eq(Rejection::getRejectStatus, rejectStatus);
        }


        // 执行查询，PageHelper会自动拦截并处理分页
        List<Rejection> rejectList = rejectionMapper.selectList(queryWrapper);

        return R.Success("查询成功", new PageInfo<>(rejectList));

    }

    private String generateRejectionId() {
        // 前缀
        String prefix = "RET";
        // 日期部分：年月日
        String datePart = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyyMMdd"));
        // 随机数部分：6位随机数
        String randomPart = String.format("%06d", new Random().nextInt(1000000));

        // 组合生成保单编号
        return prefix + datePart + randomPart;
    }

}
